﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using HRMS_Model;
using DataManager;
using System.Diagnostics;
using System.Web.Mvc;
using HRMS.Models.ModelBinders;

namespace HRMS.Controllers
{
    public class SkillsModels
    {
        public static bool AddSkill(Skill skill)
        {
            try
            {
            using (var skillEntity = new HRMSEntities())
            {
                skillEntity.Skills.AddObject(skill);
                skillEntity.SaveChanges();
                return true;
            }
            }
           catch (Exception ex)
            {
                Debug.Write(ex.Message);
                return false;
            }
        }

        public static List<Skill> Getskills(string sortExpression, string sortDirection, int pageIndex, int pageSize, int pageCount)
        {
            using (var skillEmtity = new HRMSEntities())
            {
                return skillEmtity.Skills.Include("SubSkill").OrderBy("it." + sortExpression + " " + sortDirection).Skip(pageIndex * pageSize).Take(pageCount * pageSize).ToList();
            }
        }
        public static int GetSkillsCount()
        {
            using (var skillEntity = new HRMSEntities())
            {
                return skillEntity.Skills.Count();
            }
        }

        public static bool UpdateSkill([ModelBinder(typeof(CustomModelBinder))]Skill skill)
        {
            try
            {
                using (var skillEntity = new HRMSEntities())
                {
                    var skillToEdit = skillEntity.Skills.FirstOrDefault(r => r.SkillId == skill.SkillId);
                    if (skillToEdit != null)
                    {
                        skillToEdit.SubSkillId = skill.SubSkillId;
                        skillToEdit.SkillName = skill.SkillName;
                         
                        //empToEdit.Department.DepartmentName
                    }
                    skillEntity.SaveChanges();
                    return true;
                }
            }
            catch (Exception ex)
            {
                Debug.Write(ex.Message);
                return false;
            }
        }

        public static bool DeleteSkill(int id)
        {
            try
            {
                using (var skillEntity = new HRMSEntities())
                {
                    skillEntity.Skills.DeleteObject(skillEntity.Skills.First(e => e.SkillId == id));
                    skillEntity.SaveChanges();
                    return true;
                }
            }
            catch (Exception ex)
            {
                Debug.Write(ex.Message);
                return false;
            }
        }

        public static List<string> SkillsList()
        {
            var skillsList = new List<string>();
            try
            {
                using (var skillEntity = new HRMSEntities())
                {
                    var skills = skillEntity.Skills.ToList();
                    skillsList.AddRange(skills.Select(skill => skill.SkillName));
                }
                return skillsList;
            }
            catch (Exception ex)
            {
                Debug.Write(ex.Message);
                return skillsList;
            }
        }         

    }
}